<template>
  <div class="">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          ><i class="el-icon-lx-copy"></i> 公告</el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>

    <div class="container" v-show="showSendNoticeBtn">
      <el-button type="primary" icon="el-icon-edit" @click="sendNotice()">发布公告</el-button>
    </div>

    <el-dialog v-model="showNotice" style="margin-top: 30px" title="公告" center>
        <el-card shadow="hover">
          <el-form ref="form" :model="addNotice" label-width="80px">
            <el-form-item label="标题">
              <el-input v-model="addNotice.articleTitle"></el-input>
            </el-form-item>

            <el-form-item label="内容">
              <el-input
                type="textarea"
                autosize
                placeholder="请输入内容"
                v-model="addNotice.comment"
              >
              </el-input>
            </el-form-item>

            <!-- <el-form-item label="内容简介">
                  <el-input type="textarea" autosize show-word-limit v-model="addNotice.briefComment"></el-input>
                </el-form-item> -->

            <el-form-item label="发布人">
              <el-input
                type="text"
                v-model="addNotice.creator"
                disabled
              ></el-input>
            </el-form-item>

            <el-form-item label="发布时间">
              <el-input type="text" v-model="createTime" disabled></el-input>
            </el-form-item>
            
        </el-form>
        </el-card>
        <template #footer>
          <el-button type="primary" @click="sendNotice()">取消</el-button>
          <el-button type="danger" @click="onSubmit">创建</el-button>
        </template>
      </el-dialog>

    <div id="annoucement">
      <NoticeComponents
        v-for="(notice, index) in noticeList"
        :key="index"
        :index="index"
        :notice="notice"
        @closeWin="closeWin"
        :ref="'key' + index"
      ></NoticeComponents>
    </div>
    <!-- <el-card class="news" shadow="hover">
            <div class="block" v-for="each in noticeList" :key="each.index">
                <el-row class="rowTop">
                    <el-col :span="18">
                        <div class="notice-title">
                            <span>{{each.title}}</span>
                        </div>
                    </el-col>
                    <el-col :span="6">
                        <div class="notice-author">
                            <span>{{each.publisher}}</span>
                        </div>
                    </el-col>
                </el-row>
                <el-row class="rowTop">
                    <el-col :span="24">
                        <span class="notice-content fold">
                            
                            xxxxxxxxxxxxxxxxxxxxxxx
                        </span>
                    </el-col>
                </el-row>
                <el-row class="rowTop">
                    <el-col :span="24">
                        <div>
                            <span class="notice-createTime">{{each.time}}</span>
                            <span class="notice-more">阅读全文</span>
                        </div>
                    </el-col>
                </el-row>
                <el-divider />
            </div>
        </el-card> -->

    <!-- 分页 -->
    <el-card>
      <el-pagination
        v-model:currentPage="currentPage"
        v-model:page-size="pageSize"
        background="true"
        layout="total, prev, pager, next, jumper"
        :total="parseInt(totalNum)"
        :page-sizes="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import NoticeComponents from "../components/NoticeComponents.vue";
import { ref, reactive, onMounted, getCurrentInstance } from "vue";
import { useStore } from "vuex";
import dayjs from "dayjs"; //引入时间插件
import { map } from "lodash";
import { ElMessage } from 'element-plus';
export default {
  name: "annoucement",
  components: {
    NoticeComponents,
  },

  setup() {
    const store = useStore();
    const _this = getCurrentInstance();
    const showSendNoticeBtn = ref(false);
    onMounted(() => {
      // 请求列表数据
      getNoticeList();
      store.dispatch("CurrentUserInfo").then((res) => {
          if (res.code === 200) {
              // console.log("@@@@@@",res.obj.authorities[0].authority);
              if(res.obj.authorities[0].authority==="ROLE_administrators"){
                  showSendNoticeBtn.value = true;
              }
          } else {
              console.log(res.message);
          }
      });
      // console.log("this:", getCurrentInstance().ctx.$refs);
    });

    //通知
    const message = ref("first");
    let showNotice = ref(false);
    const addNotice = reactive({
      articleTitle: "",
      comment: "",
      creator: "",
    });

    // const state = reactive({
    //   unread: [
    //     {
    //       createTime: "2022-04-19 20:00:00",
    //       title: "【系统通知】该系统将于今晚凌晨2点到5点进行升级维护",
    //       creator: "张三",
    //     },
    //     {
    //       createTime: "2022-04-21 21:00:00",
    //       title: "李四申请管理员权限",
    //       creator: "李四",
    //     },
    //   ],
    // });

    //消息
    const noticeList = reactive([]);

    let thisPageNum; // 当前页面公告数
    // 获取消息
    const getNoticeList = () => {
      store
        .dispatch("GetNoticeList", {
          page: currentPage.value,
          pageSize: pageSize.value,
        })
        .then((res) => {
          if (res.code === 200) {
            totalNum.value = res.obj.records;
            // console.log("#获取公告信息", res.obj);
            thisPageNum = res.obj.rows.length;
            noticeList.splice(
              0,
              noticeList.length,
              ...map(res.obj.rows, (item) => {
                return {
                  title: item.title,
                  time: dayjs(item.publish_date).format("YYYY-MM-DD"),
                  publisher: item.author,
                  text: item.content,
                };
              })
            );
            // console.log("@公告列表", noticeList);
          } else {
            console.log("获取公告信息失败", res.message);
          }
        });
    };

    const closeWin = (index) => {
      for (let i = 0; i < thisPageNum; i++) {
        if (i == index) continue;
        // getCurrentInstance().ctx.$refs[`key${i}`].closeWin();
        _this.ctx.$refs[`key${i}`].closeWin();
      }
    };

    let createTime = ref(
      //生成时间
      // console.log("@@@",dayjs);
      dayjs().format("YYYY-MM-DD HH:mm:ss")
    );

    const sendNotice = () => {
      // console.log("before###", showNotice);
      showNotice.value = !showNotice.value;
      // console.log("after###", showNotice);
    };

    const onSubmit = () => {
      console.log("添加内容", addNotice);
      if (addNotice.articleTitle != '' && addNotice.comment != '') {
        store
          .dispatch("SetNotice", {
            addNotice,
            createTime,
          })
          .then((res) => {
            if (res.code === 200) {
              ElMessage({
                type:"success",
                message: "添加成功",
                center:true,
              })
              showNotice.value = false;
              getNoticeList();
              // console.log("#设置公告成功", res.obj);
            } else {
              ElMessage({
                type:"error",
                message: "添加失败",
                center:true,
              })
              console.log("设置公告失败", res.message);
            }
          });
      } else {
        ElMessageBox.alert("有必要信息未填写！");
      }
    };

    const handleRead = (index) => {
      const item = state.unread.splice(index, 1);
      console.log(item);
    };

    //分页
    let currentPage = ref(1);
    let pageSize = ref(5);
    let totalNum = ref(14); //总条数除以每页显示条数10
    const handleSizeChange = (val) => {
      // console.log(`${val} items per page`)
    };
    const handleCurrentChange = (val) => {
      // console.log(`current page: ${val}`)
      getNoticeList();
    };
    //异步请求列表数据

    return {
      showSendNoticeBtn,
      addNotice,
      noticeList,
      // state,
      createTime,
      showNotice,
      message,
      sendNotice,
      onSubmit,
      handleRead,
      closeWin,

      //分页
      currentPage,
      thisPageNum,
      pageSize,
      totalNum,
      handleSizeChange,
      handleCurrentChange,
    };
  },
};
</script>

<style scoped>
.news {
  margin-top: 20px;
}

.block {
  padding-top: 10px;
  padding-bottom: 10px;
}

.notice-title {
  font-size: 18px;
  color: blue;
}

.notice-author {
  float: right;
}

.notice-content {
  line-height: 20px;
  height: 40px;
  overflow: hidden;
}
.fold {
  width: 100%;
  height: 20px;
}

.notice-more {
  float: right;
}
.rowTop {
  margin-top: 10px;
}
</style>

